//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================



using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Web.Administration;


using LitwareHR.Portfolio.Contract;
using LitwareHR.Portfolio.Services.Properties;

namespace LitwareHR.Portfolio.Services
{
    public sealed class IIS7VirtualDirectoryProvisioner
    {
        private IIS7VirtualDirectoryProvisioner()
        {            
        }

        private const string _rootPath = "/";

        public static string GetWebsiteNameById(string serverName, long parentWebsiteId)
        {
            try
            {
                 ServerManager serverManager = ServerManager.OpenRemote(serverName);
                 return GetWebSiteNameById(serverManager, parentWebsiteId);          
            }
            catch (Exception ex)
            {
                throw new ProvisioningException(Resources.VirtualDirectoryCreationFailed, ex);
            }
            
        }

        public static void AddApplication(string serverName, long parentWebsiteId, string applicationName, string physicalLocation)
        {
            try
            {
                ServerManager serverManager = ServerManager.OpenRemote(serverName);

                string siteName = GetWebSiteNameById(serverManager, parentWebsiteId);

                Site parentWebsite = serverManager.Sites[siteName];                

                if (parentWebsite != null)
                {
                    string path = String.Format("/{0}", applicationName);
                    parentWebsite.Applications.Add(path, physicalLocation);
                }

                serverManager.CommitChanges();
            }
            catch (Exception ex)
            {
                throw new ProvisioningException(Resources.VirtualDirectoryCreationFailed, ex);
            }
        }

        public static void AddVirtualFolder(string serverName, long parentWebsiteId, string virtualDirectoryName, string physicalLocation)
        {
            try
            {
                ServerManager serverManager = ServerManager.OpenRemote(serverName);

                string siteName = GetWebSiteNameById(serverManager, parentWebsiteId);
                Site parentWebsite = serverManager.Sites[siteName];               

                if (parentWebsite != null)
                {
                    Application rootApplication = GetRootApplication(parentWebsite);

                    if (rootApplication != null)
                    {
                        string path = String.Format("/{0}", virtualDirectoryName);
                        rootApplication.VirtualDirectories.Add(path, physicalLocation);
                    }
                }

                serverManager.CommitChanges();
            }
            catch (Exception ex)
            {
                throw new ProvisioningException(Resources.VirtualFolderCreationFailed, ex);
            }
        }


        private static string GetWebSiteNameById(ServerManager serverManager, long parentWebSiteId)
        {
            if (serverManager != null)
            {
                foreach (Site site in serverManager.Sites)
                {
                    if (site.Id == parentWebSiteId)
                    {
                        return site.Name;
                    }
                }
            }

            return String.Empty;

        }

        private static Application GetRootApplication(Site webSite)
        {
            if (webSite != null)
            {
                foreach (Application app in webSite.Applications)
                {
                    if (app.Path == _rootPath)
                    {
                        return app;
                    }
                }
            }

            return null;
        }
   
       
    }
}

